package com.example.ecommercedemo.controller;

//import org.springframework.security.access.prepost.PreAuthorize;
import com.example.ecommercedemo.entity.Good;
import com.example.ecommercedemo.service.GoodService;
import com.example.ecommercedemo.utils.PageResult;
import com.example.ecommercedemo.utils.PassToken;
import com.example.ecommercedemo.utils.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * goodController
 * 
 * @author ruoyi
 * @date 2025-04-03
 */
@RestController
@RequestMapping("/system/good")
public class GoodController
{
    @Autowired
    private GoodService goodService;

    /**
     * 查询good列表
     */
    @PassToken
    @GetMapping("/list")
    public ServerResponse list(Good good, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4")  int pageSize)
    {
        PageResult pageResult = (PageResult) goodService.selectGoodList(good,pageNum,pageSize);

        return ServerResponse.createBySuccess(200,pageResult,"获取成功");
    }

    /**
     * 获取good详细信息
     */
    @PassToken
    @GetMapping(value = "/{gid}")
    public ServerResponse getInfo(@PathVariable("gid") Long gid)
    {
        Good good=goodService.selectGoodByGid(gid);
        return ServerResponse.createBySuccess(200,good,"查询成功");
    }
    /**
     * 根据cid获取good详细信息
     */
//    @PassToken
//    @GetMapping(value = "/{cid}")
//    public ServerResponse selectGoodByCid(@PathVariable("cid") Long cid)
//    {
//        Good good=goodService.selectGoodByCid(cid);
//        return ServerResponse.createBySuccess(200,good,"查询成功");
//    }
    @PassToken
    //不分页查询表
    @GetMapping("/list1")
    public ServerResponse list1(Good good)
    {
        System.out.println(good.getCid());
        List<Good> list=goodService.selectGoodListBynot(good);
        return ServerResponse.createBySuccess(200,list,"获取成功");
    }

    /**
     * 新增good
     */
    @PostMapping
    public ServerResponse add(@RequestBody Good good)
    {
        int i= goodService.insertGood(good);
        if(i>0){
            return  ServerResponse.createBySuccess(200,"","添加成功");
        }else{
            return ServerResponse.createByFail(500,"添加失败");
        }
    }

    /**
     * 修改good
     */
    @PutMapping
    public ServerResponse edit(@RequestBody Good good)
    {
        int i= goodService.updateGood(good);
        if(i>0){
            return  ServerResponse.createBySuccess(200,"","更新成功");
        }else{
            return ServerResponse.createByFail(500,"更新失败");
        }
    }

//    删除good
	@DeleteMapping("/{gids}")
    public ServerResponse remove(@PathVariable Long[] gids)
    {
        int i= goodService.deleteGoodByGids(gids);
        if(i>0){
            return  ServerResponse.createBySuccess(200,"","删除成功");
        }else{
            return ServerResponse.createByFail(500,"删除失败");
        }
    }
}
